<template>
    <div class="h-full flex-col">
        <SearchForm @handle-reset="resetParams" @handle-search="getList">
            <el-form :model="searchForm" inline>
                <el-form-item>
                    <el-input v-model="searchForm.shop_id" clearable @change="getList" class="w-220px" placeholder="店铺ID" />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="searchForm.shop_name"  clearable @change="getList" class="w-220px" placeholder="店铺名称" />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="searchForm.shop_phone"  clearable @change="getList"  class="w-220px" placeholder="客服手机号" />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="searchForm.user_phone"  clearable @change="getList"  class="w-220px" placeholder="用户手机号" />
                </el-form-item>
                <el-form-item>
                    <el-select v-model="searchForm.isBuy" placeholder="订购状态" clearable @change="getList" class="w-220px">
                        <el-option :label="item.label" :value="item.value" v-for="item in buyState" :key="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="searchForm.isProxy" placeholder="是否存在代理" clearable @change="getList" class="w-220px">
                        <el-option label="否" :value="0" />
                        <el-option label="是" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="searchForm.proxyPhone" class="w-220px" clearable @change="getList" placeholder="代理手机号" />
                </el-form-item>
            </el-form>
        </SearchForm>
        <div class="page-card h-full" v-loading="loading">
            <el-table :data="tableData">
                <el-table-column label="ID" prop="_id" align="center" />
                <el-table-column label="店铺名称" prop="shop_name" align="center" />
                <el-table-column label="店铺ID" prop="shop_id" align="center" />
                <el-table-column label="开业时间" prop="openForBusinessTime" align="center">
                    <template #default="{ row }">
                        {{ row.openForBusinessTime || '/' }}
                    </template>
                </el-table-column>
                <el-table-column label="店铺客服手机号" prop="shop_phone" align="center">
                    <template #default="{ row }">
                        {{ row.shop_phone || '/' }}
                    </template>
                </el-table-column>
                <el-table-column label="用户手机号" prop="user_phone" align="center">
                    <template #default="{ row }">
                        {{ row.user_phone || '/' }}
                    </template>
                </el-table-column>
                <el-table-column label="运营名" prop="operator_name" align="center">
                    <template #default="{ row }">
                        {{ row.operator_name || '/' }}
                    </template>
                </el-table-column>
                <el-table-column label="推广链接ID" prop="proxyLinkId" align="center">
                    <template #default="{ row }">
                        <p v-if="row.proxyLinkId">
                            {{ row.proxyLinkId.identification }}
                            <el-link type="primary" @click="gotolinkpage(row.proxyLinkId)">查看</el-link>
                        </p>
                        <p v-else> / </p>
                    </template>
                </el-table-column>
                <el-table-column label="代理名" prop="proxyId" align="center">
                    <template #default="{ row }">
                        <p v-if="row.proxyId">
                            {{ row.proxyId.userName }}
                            <el-link type="primary" @click="gotoadminuserpage(row.proxyId)">查看</el-link>
                        </p>
                        <p v-else> / </p>
                    </template>
                </el-table-column>
                <el-table-column label="代理手机号" prop="proxyId.phone" align="center" />
                <!-- <el-table-column label="父账号状态" align="center" >
                    <template #default="{ row }">
                        {{ row.status ? '正常' : '禁用'}}
                    </template>
                </el-table-column> -->
                <el-table-column label="是否订购" prop="isBuy" align="center">
                    <template #default="{ row }">
                        {{ getBuyLabel(row.isBuy) }}
                    </template>
                </el-table-column>
                <el-table-column label="剩余时间" prop="phone" align="center" />
                <el-table-column label="创建时间" prop="createTime" align="center" />
                <el-table-column label="操作" prop="isBuy" align="center">
                    <template #default="{ row }">
                        <el-link type="primary" @click="returnVisit(row)">回访</el-link>
                    </template>
                </el-table-column>
            </el-table>
            <Pagination :pageable="page" :handleSizeChange="handleSizeChange" :handleCurrentChange="handleCurrentChange" />
        </div>
    </div>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { useTable } from '@/hooks/useTable'
import { shopListApi } from '@/api/shop'
import { useRouter } from "vue-router";
const router = useRouter()

const buyState = [{
    label: '未订购',
    value: 0
}, {
    label: '订购付费',
    value: 1
}, {
    label: '订购试用',
    value: 2
}, {
    label: '订购过期',
    value: 3
}, {
    label: '已退款',
    value: 4
}]

const getBuyLabel = (val: number) => {
    return buyState.find(item => item.value === val)?.label
}

const returnVisit = (row: any) => {
    console.log(row);
}

const searchForm = reactive({
    phone: '',
    shop_id: '',
    shop_name: '',
    shop_phone:'',
    user_phone:'',
    isBuy: '',
    isProxy: '',
    proxyPhone: ''
});
const { tableData, page, loading, resetParams, getList, handleCurrentChange, handleSizeChange } =
    useTable({
        apiFn: shopListApi,
        params: searchForm
    });
const gotolinkpage = (row: any) => {
    console.log(row)
    router.push({
        path: '/promotion/link',
        query: {
            id: row._id
        }
    })
}
const gotoadminuserpage = (row: any) => {
    console.log(row)
    router.push({
        path: '/subaccount/agents',
        query: {
            id: row._id
        }
    })
}

</script>

<style lang="scss" scoped></style>
